﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DAL;
using System.Security.Cryptography;
using System.Reflection;
using System.IO;

namespace source
{
    public partial class LoginForma : Form
    {
        public LoginForma()
        {
            InitializeComponent();
        }
     
        private void LoginForma_Load(object sender, EventArgs e)
        {
            label3.Text = "Spremno";
        }

        public static string hashSHA512(string unhashedValue)
        {
            SHA512 shaM = new SHA512Managed();
            byte[] hash =
            shaM.ComputeHash(Encoding.ASCII.GetBytes(unhashedValue));
            StringBuilder stringBuilder = new StringBuilder();
            foreach (byte b in hash)
                stringBuilder.AppendFormat("{0:x2}", b);
            return stringBuilder.ToString();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            String name = textBox1.Text;
            String pass = textBox2.Text;
            if ("" == name || "" == pass)
            {
                label3.Text = "Podaci nisu unešeni!";
                statusStrip1.BackColor = Color.OrangeRed;
                return;
            }
            pass = hashSHA512(pass);
            DAL.DAL dal = DAL.DAL.Instanca;
            dal.kreirajKonekciju("127.0.0.1", "skola", "root", "");
            DAL.DAL.KorisnikDAO dao = new DAL.DAL.KorisnikDAO();
            Korisnik user = dao.returnLogIn(name, pass);
            dal.terminirajKonekciju();
            if (user.Ime == "Prazan")
            {
                label3.Text = "Unešeni su pogrešni podaci!";
                statusStrip1.BackColor = Color.OrangeRed;
                return;
            }
            String type = user.Tip;
            if (type == "Ucenik")
            {
                dal.kreirajKonekciju("127.0.0.1", "skola", "root", "");
                DAL.DAL.UcenikDAO u = new DAL.DAL.UcenikDAO();
                Ucenik ucenik = u.getById(user.ID);
                UcenikForma UF = new UcenikForma(ucenik);
                UF.ShowDialog();
                return;
            }
            else if (type == "Profesor")
            {
                dal.kreirajKonekciju("127.0.0.1", "skola", "root", "");
                DAL.DAL.ProfesorDAO p = new DAL.DAL.ProfesorDAO();
                Profesor prof = p.getById(user.ID);
                ProfesorForma pf = new ProfesorForma(prof);
                pf.ShowDialog();
                dal.terminirajKonekciju();
                return;
            }
            else if (type == "Pedagog")
            {
                dal.kreirajKonekciju("127.0.0.1", "skola", "root", "");
                DAL.DAL.PedagogDAO pg = new DAL.DAL.PedagogDAO();
                Pedagog pedi = pg.getById(user.ID);
                PedagogForma pedForm = new PedagogForma(pedi);
                pedForm.ShowDialog();
                return;
            }
            else if (type == "Direktor")
            {
                dal.kreirajKonekciju("127.0.0.1", "skola", "root", "");
                DAL.DAL.DirektorDAO d = new DAL.DAL.DirektorDAO();
                Direktor dir = d.getById(user.ID);
                DirektorForma df = new DirektorForma(dir);
                df.Show();
                dal.terminirajKonekciju();
                return;
            }
            else if(type == "Administrator")
            {
                AdministratorForma af = new AdministratorForma(user);
                af.ShowDialog();
                return;
            }
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            label3.Text = "Spremno";
            statusStrip1.BackColor = SystemColors.MenuHighlight;
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {
            label3.Text = "Spremno";
            statusStrip1.BackColor = SystemColors.MenuHighlight;
        }
        
        private void textBox2_KeyDown(object sender, KeyEventArgs e)
        {
            if ((e as KeyEventArgs).KeyCode == Keys.Enter)
                button1.PerformClick();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

     }
}
